library(pwr)
library(MBESS)
library(gdata)
library(tidyverse)
data <- read.table("/Users/redviper/Documents/flagged_revs_dataset.tsv",header=T)
# Examine the dataset within RStudio
model <- lm(anon_reverted_rate_std_unit ~ month + level + trend +  wiki:month, data=data)
summary(model)
level_coef <- coef(summary(model))[3, 1] #coefficient of flaggedrev_on
print(level_coef)
nSims <- 2000 #number of simulated experiments
data$anon_reverted_std_unit_bt <- data$anon_reverted_std_unit - (level_coef - 0.8)*data$level #zeroing out the effect and add back 0.2 sd
signif1 <- rep(NA, nSims)
for(i in 1:nSims){ #for each simulated experiment
  
  wiki_list <- resample(data$wiki, 20, replace = TRUE, prob = NULL) #resample based on wiki
  table = data.frame()
  for (w in wiki_list) {
    sample = data %>% filter(data$wiki == w)
    table <- rbind(table, sample)
  }
  model_simulation <- lm(anon_reverted_std_unit_bt ~ month + level + trend +  wiki:month, data=table)
  p1 <- coef(summary(model_simulation))[3, 4]
  signif1[i] <- p1 <=.05
}

power <- sum(signif1)/nSims
print(power)
